﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using Raize.CodeSiteLogging;
using Pub;
using RainMgr;

namespace LZQXT.Test.Forms
{
    public partial class FOptRain : Form
    {
        ADOTest ado1;

        public FOptRain()
        {
            InitializeComponent();
        }

        private void FOptRain_Load(object sender, EventArgs e)
        {
            init();
        }

        public void init()
        {
            try
            {
                ado1 = new ADOTest();
                ado1.S_Con = CConfig.getPermite("DBCon");
                // 站点维护
                string strSQL = "SELECT * FROM P_Product_AppField WHERE F_Name = 'rain'";
                DataSet ds1 = ado1.sel1(strSQL);
                listBox1.DataSource = ds1.Tables[0];
                listBox1.DisplayMember = "Oth";

                // 权限维护
                string strSQL2 = "SELECT * FROM P_USER";
                DataSet ds2 = ado1.sel1(strSQL2);
                listBox2.DataSource = ds2.Tables[0];
                listBox2.DisplayMember = "FNAME";

                // 登录权限
                string strSQL3 = "SELECT * FROM P_USER";
                DataSet ds3 = ado1.sel1(strSQL3);
                listBox3.DataSource = ds3.Tables[0];
                listBox3.DisplayMember = "FNAME";

                // 自动站与人工站设置
                refStaType();
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
                MessageBox.Show(ex.ToString());
            }

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.Text == string.Empty) return;

            refStaInfo(listBox1.Text);
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox2.Text == string.Empty) return;

            refStaInfo_User(listBox2.Text);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            saveStaID();
        }

        /// <summary>
        /// 保存雨情点权限信息——按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_UserOpt_Click(object sender, EventArgs e)
        {
            saveStaID_User();
        }

        /// <summary>
        /// 刷新雨情快报站点配置信息
        /// </summary>
        /// <param name="P_Name"></param>
        private void refStaInfo(string P_Name)
        {
            //CDebug.EnterMethod("刷新雨情站点");
            try
            {
                string strSQL = "SELECT * FROM P_Product_AppField WHERE F_Name = 'rain' AND Oth like '" + P_Name + "'";
                DataSet ds1 = ado1.sel1(strSQL);
                //CDebug.Send(strSQL + "\n--返回结果个数: " + ado1.ds_count);
                if (!CDB.dsIsNull(ds1))
                {
                    string P_ID = ds1.Tables[0].Rows[0]["P_ID"].ToString();

                    /*
                    string strSQL2 = "SELECT A.*, B.StaID FROM ( "
                        + "SELECT A.StationName AS 县区,B.StationName AS 站点,B.StationID AS 站号 FROM P_RainStation_Config A "
                        + "RIGHT JOIN P_RainStation_Config B ON A.ID = B.ParentID AND A.ParentID=-1 WHERE B.StationID <> '' "
                        + ") A LEFT JOIN P_Rain_Grid B ON A.站号=B.StaID AND B.P_ID='" + P_ID + "'";
                    DataSet ds2 = ado1.sel1(strSQL);
                    */
                    treeView1.Nodes.Clear();
                    //填充县区名称
                    string strSQL2 = "SELECT * FROM P_RainStation_Config WHERE ParentID=-1";
                    DataSet ds2 = ado1.sel1(strSQL2);
                    for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds2.Tables[0].Rows[i];
                        string sXQ = dr1["StationName"].ToString();
                        string ID = dr1["ID"].ToString();
                        TreeNode node1 = new TreeNode();
                        node1.Text = sXQ;
                        node1.Tag = "";

                        //填充站点名称
                        string strSQL3 = "SELECT A.*,B.StaID FROM P_RainStation_Config A LEFT JOIN P_Rain_Grid B ON A.StationID=B.StaID "
                            + " AND B.P_ID=" + P_ID + " WHERE A.ParentID=" + ID;
                        DataSet ds3 = ado1.sel1(strSQL3);
                        for (int j = 0; j < ds3.Tables[0].Rows.Count; j++)
                        {
                            DataRow dr3 = ds3.Tables[0].Rows[j];
                            string sStaName = dr3["StationName"].ToString();
                            string sStaID = dr3["StationID"].ToString();
                            string sChk = dr3["StaID"].ToString();
                            TreeNode node2 = new TreeNode();
                            node2.Text = sStaName;
                            node2.Tag = sStaID;
                            if (sChk != string.Empty) node2.Checked = true;

                            node1.Nodes.Add(node2);
                        }

                        node1.Expand();
                        treeView1.Nodes.Add(node1);
                    }
                }

            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
            }
            //CDebug.ExitMethod("刷新雨情站点");
        }

        /// <summary>
        /// 刷新雨情点权限信息
        /// </summary>
        /// <param name="s_User"></param>
        private void refStaInfo_User(string s_User)
        {
            //CDebug.EnterMethod("刷新雨情站点权限信息");
            try
            {
                string strSQL = "SELECT * FROM P_User WHERE FName like '" + s_User + "'";
                DataSet ds1 = ado1.sel1(strSQL);
                //CDebug.Send(strSQL + "\n--返回结果个数: " + ado1.ds_count);
                if (!CDB.dsIsNull(ds1))
                {
                    string UID = ds1.Tables[0].Rows[0]["UID"].ToString();

                    /*
                    string strSQL2 = "SELECT A.*, B.StaID FROM ( "
                        + "SELECT A.StationName AS 县区,B.StationName AS 站点,B.StationID AS 站号 FROM P_RainStation_Config A "
                        + "RIGHT JOIN P_RainStation_Config B ON A.ID = B.ParentID AND A.ParentID=-1 WHERE B.StationID <> '' "
                        + ") A LEFT JOIN P_Rain_Grid B ON A.站号=B.StaID AND B.P_ID='" + P_ID + "'";
                    DataSet ds2 = ado1.sel1(strSQL);
                    */
                    treeView2.Nodes.Clear();
                    TreeNode node_root = new TreeNode();
                    node_root.Text = "\\";
                    node_root.Tag = "";
                    //填充县区名称
                    string strSQL2 = "SELECT * FROM P_RainStation_Config WHERE ParentID=-1";
                    DataSet ds2 = ado1.sel1(strSQL2);
                    for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds2.Tables[0].Rows[i];
                        string sXQ = dr1["StationName"].ToString();
                        string ID = dr1["ID"].ToString();
                        TreeNode node1 = new TreeNode();
                        node1.Text = sXQ;
                        node1.Tag = "";

                        //填充站点名称
                        string strSQL3 = "SELECT A.* FROM P_RainStation_Config A WHERE A.ParentID=" + ID;
                        DataSet ds3 = ado1.sel1(strSQL3);
                        for (int j = 0; j < ds3.Tables[0].Rows.Count; j++)
                        {
                            DataRow dr3 = ds3.Tables[0].Rows[j];
                            string sStaName = dr3["StationName"].ToString();
                            string sStaID = dr3["StationID"].ToString();
                            string sEdtUsr = dr3["EdtUsr"].ToString();
                            TreeNode node2 = new TreeNode();
                            node2.Text = sStaName;
                            node2.Tag = sStaID;
                            if (sEdtUsr.IndexOf("#" + UID + "#") != -1) node2.Checked = true;

                            node1.Nodes.Add(node2);
                        }

                        node1.Expand();
                        node_root.Nodes.Add(node1);
                        node_root.Expand();
                    }
                    treeView2.Nodes.Add(node_root);
                }

            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
            }
            //CDebug.ExitMethod("刷新雨情站点权限信息");
        }

        /// <summary>
        /// 保存雨情快报站点设置
        /// </summary>
        private void saveStaID()
        {
            //CDebug.EnterMethod("更新雨情站点数据");
            try
            {
                string strSQL = "SELECT * FROM P_Product_AppField WHERE F_Name = 'rain' AND Oth like '" + listBox1.Text + "'";
                DataSet ds1 = ado1.sel1(strSQL);
                string P_ID = ds1.Tables[0].Rows[0]["P_ID"].ToString();

                foreach (TreeNode nodeXQ in treeView1.Nodes)
                {
                    foreach (TreeNode node1 in nodeXQ.Nodes)
                    {
                        string staID = "";
                        string staName = "";

                        if (node1.Tag.ToString() != string.Empty)
                        {
                            staID = node1.Tag.ToString();
                            staName = node1.Text;

                            string strSQL2 = "SELECT * FROM P_Rain_Grid WHERE P_ID=" + P_ID + " AND StaID='" + staID + "'";
                            DataSet ds2 = ado1.sel1(strSQL2);
                            if (CDB.dsIsNull(ds2) && node1.Checked == true)
                            {
                                string sID_New = ado1.sel1("select Isnull(Max(Convert(int,ID)),0) + 1 as s1 from P_Rain_Grid;").Tables[0].Rows[0][0].ToString();
                                string strSQL3 = "INSERT INTO P_Rain_Grid (ID,P_ID,Tab_ID,StaID,Oth) VALUES("
                                    + sID_New + "," + P_ID + ",0,'" + staID + "','" + staName + "')";
                                //CDebug.Send(strSQL3);
                                ado1.cmd1(strSQL3);
                            }
                            else if (!CDB.dsIsNull(ds2) && node1.Checked == false)
                            {
                                string strSQL3 = "DELETE FROM P_Rain_Grid WHERE P_ID=" + P_ID + " AND StaID='" + staID + "'";
                                //CDebug.Send(strSQL3);
                                ado1.cmd1(strSQL3);
                            }
                        }
                    }
                }

                MessageBox.Show("保存成功!");
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
            }
            //CDebug.ExitMethod("修改雨情站点数据");
        }

        /// <summary>
        /// 保存雨情点权限信息
        /// </summary>
        private void saveStaID_User()
        {
            //CDebug.EnterMethod("更新雨情站点权限");
            try
            {
                string strSQL = "SELECT * FROM P_User WHERE FName like '" + listBox2.Text + "'";
                DataSet ds1 = ado1.sel1(strSQL);
                string UID = ds1.Tables[0].Rows[0]["UID"].ToString();

                foreach (TreeNode nodeXQ in treeView2.Nodes[0].Nodes)
                {
                    foreach (TreeNode node1 in nodeXQ.Nodes)
                    {
                        string staID = "";
                        string staName = "";

                        if (node1.Tag.ToString() != string.Empty)
                        {
                            staID = node1.Tag.ToString();
                            staName = node1.Text;

                            string strSQL2 = "SELECT * FROM P_RainStation_Config WHERE StationID='" + staID + "'";
                            DataSet ds2 = ado1.sel1(strSQL2);
                            if (!CDB.dsIsNull(ds2))
                            {
                                string sEdtUsr = ds2.Tables[0].Rows[0]["EdtUsr"].ToString();
                                string sID = ds2.Tables[0].Rows[0]["ID"].ToString();
                                if (sEdtUsr.IndexOf("#" + UID + "#") == -1 && node1.Checked == true)
                                {
                                    if (sEdtUsr == null || sEdtUsr == string.Empty) sEdtUsr += "#";
                                    string sUsr_Tem = sEdtUsr + UID + "#";
                                    string strSQL3 = "UPDATE P_RainStation_Config SET EdtUsr='" + sUsr_Tem + "' WHERE ID=" + sID;
                                    //CDebug.Send(strSQL3);
                                    ado1.cmd1(strSQL3);
                                }
                                else if (sEdtUsr.IndexOf("#" + UID + "#") != -1 && node1.Checked == false)
                                {
                                    string sUsr_Tem = sEdtUsr.Replace("#" + UID + "#", "#");
                                    string strSQL3 = "UPDATE P_RainStation_Config SET EdtUsr='" + sUsr_Tem + "' WHERE ID=" + sID;
                                    //CDebug.Send(strSQL3);
                                    ado1.cmd1(strSQL3);
                                }
                            }

                        }
                    }
                }

                MessageBox.Show("保存成功!");
            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
            }
            //CDebug.ExitMethod("修改雨情站点权限信息");
        }

        private void treeView2_AfterCheck(object sender, TreeViewEventArgs e)
        {
            // The code only executes if the user caused the checked state to change.
            if (e.Action != TreeViewAction.Unknown)
            {
                if (e.Node.Nodes.Count > 0)
                {
                    /* Calls the CheckAllChildNodes method, passing in the current  
                    Checked value of the TreeNode whose checked state changed. */
                    this.CheckAllChildNodes(e.Node, e.Node.Checked);
                }
            }
        }

        // Updates all child tree nodes recursively.
        private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                node.Checked = nodeChecked;
                if (node.Nodes.Count > 0)
                {
                    // If the current node has child nodes, call the CheckAllChildsNodes method recursively.
                    this.CheckAllChildNodes(node, nodeChecked);
                }
            }
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Action != TreeViewAction.Unknown)
            {
                if (e.Node.Nodes.Count > 0)
                {
                    /* Calls the CheckAllChildNodes method, passing in the current  
                    Checked value of the TreeNode whose checked state changed. */
                    this.CheckAllChildNodes(e.Node, e.Node.Checked);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (TreeNode node1 in treeView3.Nodes[0].Nodes)
                {

                    string s_key = "";
                    string s_name = "";

                    if (node1.Tag.ToString() != string.Empty)
                    {
                        s_key = node1.Tag.ToString();
                        s_name = node1.Text;
                        string strSQL = "SELECT * FROM P_User WHERE FName like '" + listBox3.Text + "'";
                        DataSet ds1 = ado1.sel1(strSQL);


                        if (!CDB.dsIsNull(ds1))
                        {
                            string s_rule = ds1.Tables[0].Rows[0]["rule"].ToString();
                            string UID = ds1.Tables[0].Rows[0]["UID"].ToString();
                            if (s_rule.IndexOf("#" + s_key + "#") == -1 && node1.Checked == true)
                            {
                                if (s_rule == null || s_rule == string.Empty) s_rule += "#";
                                string sRule_Tem = s_rule + s_key + "#";
                                string strSQL3 = "UPDATE P_User SET [rule]='" + sRule_Tem + "' WHERE UID=" + UID;
                                //CDebug.Send(strSQL3);
                                ado1.cmd1(strSQL3);
                            }
                            else if (s_rule.IndexOf("#" + s_key + "#") != -1 && node1.Checked == false)
                            {
                                string sRule_Tem = s_rule.Replace("#" + s_key + "#", "#");
                                string strSQL3 = "UPDATE P_User SET [rule]='" + sRule_Tem + "' WHERE UID=" + UID;
                                //CDebug.Send(strSQL3);
                                ado1.cmd1(strSQL3);
                            }
                        }


                    }
                }

                MessageBox.Show("保存成功!");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                //CDebug.SendError(ex.ToString());
            }
        }

        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox3.Text == string.Empty) return;
            try
            {
                string[] s_key = { "101", "102", "103", "104", "105" };
                string[] s_name = { "数据共享", "预报预警", "决策服务", "农业气候", "污染预报" };
                string s_User = listBox3.Text;
                string strSQL = "SELECT * FROM P_User WHERE FName like '" + s_User + "'";
                DataSet ds1 = ado1.sel1(strSQL);
                //CDebug.Send(strSQL + "\n--返回结果个数: " + ado1.ds_count);
                if (!CDB.dsIsNull(ds1))
                {
                    string rule = ds1.Tables[0].Rows[0]["RULE"].ToString();

                    /*
                    string strSQL2 = "SELECT A.*, B.StaID FROM ( "
                        + "SELECT A.StationName AS 县区,B.StationName AS 站点,B.StationID AS 站号 FROM P_RainStation_Config A "
                        + "RIGHT JOIN P_RainStation_Config B ON A.ID = B.ParentID AND A.ParentID=-1 WHERE B.StationID <> '' "
                        + ") A LEFT JOIN P_Rain_Grid B ON A.站号=B.StaID AND B.P_ID='" + P_ID + "'";
                    DataSet ds2 = ado1.sel1(strSQL);
                    */
                    treeView3.Nodes.Clear();
                    TreeNode node_root = new TreeNode();
                    node_root.Text = "\\";
                    node_root.Tag = "";

                    for (int i = 0; i < s_key.Length; i++)
                    {

                        TreeNode node1 = new TreeNode();
                        node1.Text = s_name[i];
                        node1.Tag = s_key[i];
                        if (rule.IndexOf("#" + s_key[i] + "#") != -1) node1.Checked = true;

                        node_root.Nodes.Add(node1);
                        node_root.Expand();
                    }
                    treeView3.Nodes.Add(node_root);
                }

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                //CDebug.SendError(ex.ToString());
            }
        }

        /// <summary>
        /// 刷新自动站与人工站设置
        /// </summary>
        private void refStaType()
        {
            try
            {

                treeView4.Nodes.Clear();
                TreeNode node_root = new TreeNode();
                node_root.Text = "\\";
                node_root.Tag = "";
                //填充县区名称
                string strSQL2 = "SELECT * FROM P_RainStation_Config WHERE ParentID=-1";
                DataSet ds2 = ado1.sel1(strSQL2);
                for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                {
                    DataRow dr1 = ds2.Tables[0].Rows[i];
                    string sXQ = dr1["StationName"].ToString();
                    string ID = dr1["ID"].ToString();
                    TreeNode node1 = new TreeNode();
                    node1.Text = sXQ;
                    node1.Tag = "";

                    //填充站点名称
                    string strSQL3 = "SELECT A.* FROM P_RainStation_Config A WHERE A.ParentID=" + ID;
                    DataSet ds3 = ado1.sel1(strSQL3);
                    for (int j = 0; j < ds3.Tables[0].Rows.Count; j++)
                    {
                        DataRow dr3 = ds3.Tables[0].Rows[j];
                        string sStaName = dr3["StationName"].ToString();
                        string sStaID = dr3["StationID"].ToString();
                        string sEdtUsr = dr3["EdtUsr"].ToString();
                        TreeNode node2 = new TreeNode();
                        node2.Text = sStaName;
                        node2.Tag = sStaID;
                        //if (sEdtUsr.IndexOf("#" + UID + "#") != -1) node2.Checked = true;
                        if (dr3["TYP"].ToString() == "2")
                            node2.BackColor = Color.Red;
                        else if (dr3["TYP"].ToString() == "0")
                            node2.BackColor = Color.Yellow;

                        node1.Nodes.Add(node2);
                    }

                    node1.Expand();
                    node_root.Nodes.Add(node1);
                    node_root.Expand();
                }
                treeView4.Nodes.Add(node_root);


            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
            }
            //CDebug.ExitMethod("刷新雨情站点权限信息");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (TreeNode nodeXQ in treeView4.Nodes[0].Nodes)
                {
                    foreach (TreeNode node1 in nodeXQ.Nodes)
                    {
                        if (node1.Checked)
                        {
                            String s_TYP = "1";
                            if (radioBtn1.Checked) s_TYP = "1";
                            if (radioBtn2.Checked) s_TYP = "2";
                            if (radioBtn3.Checked) s_TYP = "0";

                            String staID = node1.Tag.ToString();
                            String staName = node1.Text;

                            string strSQL2 = "SELECT * FROM P_RainStation_Config WHERE StationID='" + staID + "'";
                            DataSet ds2 = ado1.sel1(strSQL2);
                            if (!CDB.dsIsNull(ds2))
                            {
                                string sID = ds2.Tables[0].Rows[0]["ID"].ToString();
                                string strSQL3 = "UPDATE P_RainStation_Config SET TYP='" + s_TYP + "' WHERE ID=" + sID;
                                //CDebug.Send(strSQL3);
                                ado1.cmd1(strSQL3);

                            }
                        }
                    }
                }

                refStaType();
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btn_Other_Click(object sender, EventArgs e)
        {
            FOpt_zy f = new FOpt_zy();
            f.ShowDialog();
        }
    }
}
